package ru.sviridov.sellandbuy;

import ru.sviridov.sellandbuy.model.query.QueryRegister;
import ru.sviridov.sellandbuy.tasks.base.BaseTask;
import ru.sviridov.sellandbuy.tasks.recipients.RegisterRecipient;
import ru.sviridov.sellandbuy.tools.Utils;
import ru.sviridov.sellandbuy.url.APIGroup;
import ru.sviridov.sellandbuy.url.APIMethod;
import ru.sviridov.sellandbuy.url.URL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity{
	
	public static final String LOGIN = "";

	private BaseTask<Void> task;
	
	private EditText etUserName;
	private EditText etEmail;
	private EditText etPassword;
	private EditText etQuestion;
	private EditText etAnswer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ContentViewMapper.setContentView(this);
		
		configEdits();
	}
	
	private void configEdits() {
		final View btnRegister = findViewById(R.id.btnRegister);
		etUserName = (EditText) findViewById(R.id.etUserName);
		etEmail = (EditText) findViewById(R.id.etEmail);
		etPassword = (EditText) findViewById(R.id.etPassword);
		etQuestion = (EditText) findViewById(R.id.etQuestion);
		etAnswer = (EditText) findViewById(R.id.etAnswer);
		TextWatcher watcher = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				btnRegister.setEnabled(isBtnEnabled());
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			public void afterTextChanged(Editable s) {}
		};
		
		etUserName.addTextChangedListener(watcher);
		etEmail.addTextChangedListener(watcher);
		etPassword.addTextChangedListener(watcher);
		etQuestion.addTextChangedListener(watcher);
		etAnswer.addTextChangedListener(watcher);	
		
		etAnswer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
				if (isBtnEnabled() && (id == R.id.register || id == EditorInfo.IME_ACTION_DONE)) {
					onClickRegister(etAnswer);
					return true;
				}
				return false;
			}
		});
	}
	
	private boolean isBtnEnabled(){
		return !(TextUtils.isEmpty(etUserName.getText()) || TextUtils.isEmpty(etEmail.getText()) || 
		TextUtils.isEmpty(etPassword.getText()) || TextUtils.isEmpty(etQuestion.getText()) ||
		TextUtils.isEmpty(etAnswer.getText()));
	}

	public void onClickRegister(View view){
		if(task!=null && task.getStatus()!=AsyncTask.Status.FINISHED)
			return;

		QueryRegister query = new QueryRegister();
		query.setUserName(etUserName.getText().toString());
		query.setEmail(etEmail.getText().toString());
		query.setPasswordMd5(Utils.md5(etPassword.getText().toString()));
		query.setSecretQuestion(etQuestion.getText().toString());
		query.setSecretAnswer(etAnswer.getText().toString());
		
		Toast.makeText(this, "Registering...", Toast.LENGTH_LONG).show();
		task = new BaseTask<Void>(this, new RegisterRecipient());
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.MEMBERSHIP, APIMethod.REGISTER));
	}
}
